package javaweblearn;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

/**
 * Servlet implementation class CardServlet
 */
@WebServlet("/Card")
public class CardServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CardServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		//int id = Integer.parseInt(request.getParameter("id"));
		int id=1;
		Map<String, Object> resultMap = new HashMap<>();

		

		// 2.建立数据连接
		Connection conn;
		try {
			conn = DBUtils.getConnection();

			// 3.执行SQL语句

			response.setCharacterEncoding("utf-8");
			response.setContentType("text/json; charset=UTF-8");

			String insertSQL = "select * from register where id = ?";
			PreparedStatement pstmt = conn.prepareStatement(insertSQL);
			pstmt.setInt(1, id);
			ResultSet result = pstmt.executeQuery();


			// 获取返回数据

			while (result.next()) {
				resultMap.put("account", result.getString("account"));
				resultMap.put("password", result.getString("password"));
				resultMap.put("avt", result.getString("avt"));
				resultMap.put("register_time", result.getString("register_time"));
			}

			// 5.关闭连接
			result.close();
			pstmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		PrintWriter out = response.getWriter();
		out.append(JSON.toJSONString(resultMap));
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String title = request.getParameter("title");
		String subtitle = request.getParameter("subtitle");
		String content = request.getParameter("content");

		// 2.建立数据连接
		Connection conn;
		try {
			conn = DBUtils.getConnection();

			// 3.执行SQL语句
			// 建立Statement对象
			Statement stmt = conn.createStatement();

			// 做好准备工作之后就可以执行sql语句了
			List<String> columns = new ArrayList<>();
			columns.add("title");

			// 执行sql语句
			String selectSQL = DBUtils.getSelectSQL("card", columns, " title = '" + title + "'");
			System.out.println(selectSQL);
			ResultSet rs = stmt.executeQuery(selectSQL);

			// 4.处理数据
			boolean exists = false;
			while (rs.next()) {
				exists = true;
				System.out.println("已存在：" + rs.getString("title"));
			}

			response.setCharacterEncoding("utf-8");
			if (!exists) {

				String insertSQL = "insert into card(title,subtitle,img_path,content) values(?,?,?,?)";
				PreparedStatement pstmt = conn.prepareStatement(insertSQL);
				pstmt.setString(1, title);
				pstmt.setString(2, subtitle);
				pstmt.setString(3, title);
				pstmt.setString(4, content);
				int insertResult = pstmt.executeUpdate();

				if (insertResult > 0) {
					response.getWriter().write("{'ok':true}");
				} else {
					response.getWriter().write("{'ok':false}");
				}

				pstmt.close();
			} else {
				response.getWriter().write("{'ok':false,'error':'已存在同名的'}");
			}

			// 5.关闭连接
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
